//
//  suggestViewController.swift
//  专业课
//
//  Created by 梓简王 on 2017/8/24.
//  Copyright © 2017年 梓简王. All rights reserved.
//

import Foundation
import TextFieldEffects
import QorumLogs
import Alamofire

class suggestViewController: UIViewController,headerViewDelegate,UITextViewDelegate {
    var header:headerView?
    var detailInsertTextField:UITextView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = commonVariable.BACKGROUNDCOLOR
        header = headerView(frame: commonVariable.HEADERPOSITION, leftBtnString: "< 返回", title: "建 议", rightBtnString: "上传")
        header?.delegate = self
        
        //详情输入界面
        //通过富文本来设置行间距
        let paraph = NSMutableParagraphStyle()
        //将行间距设置为28
        paraph.lineSpacing = 10
        //样式属性集合
        let attributes = [NSFontAttributeName:UIFont.systemFontOfSize(13),NSParagraphStyleAttributeName: paraph,NSForegroundColorAttributeName:commonVariable.IRONCOLOR]
        
        detailInsertTextField  = UITextView(frame: CGRect(x: 20, y: 80, width: commonVariable.SYSTEM_WIDTH - 40, height: commonVariable.SYSTEM_HEIGHT/2))
        detailInsertTextField?.attributedText = NSAttributedString(string: "", attributes: attributes)
        detailInsertTextField?.contentMode = .TopLeft
        detailInsertTextField?.font = UIFont(name: "Helvetica-Bold", size: 15)
        detailInsertTextField?.delegate = self
        
        self.view.addSubview(self.detailInsertTextField!)
        self.view.addSubview(self.header!)
    }
    func clickRightBtn() {
        self.detailInsertTextField?.resignFirstResponder()
        let tempSuggest = suggestRecord()
        tempSuggest.userID = (commonVariable.LOCAL_USER?.id)!
        tempSuggest.feedback = detailInsertTextField?.text!
        let body = ["userID":"\(tempSuggest.userID!)","feedback":"\(tempSuggest.feedback!)"]
        
        self.view.userInteractionEnabled = false
        commonVariable.LOADING_PACMAN.center = commonVariable.WINDOWSCENTER
        self.view.addSubview(commonVariable.LOADING_PACMAN)
        commonVariable.LOADING_PACMAN.startAnimation()
        
        Alamofire.request(.POST, commonVariable.USERFEEDBACK,parameters:body,headers:nil).responseJSON() {
            response in
            
            self.view.userInteractionEnabled = true
            commonVariable.LOADING_PACMAN.stopAnimation()
            commonVariable.LOADING_PACMAN.removeFromSuperview()
            
            guard let JSONDIC = response.result.value else {
                QL4(response.result.error)
                switch response.result.error!.code {
                case -1009 :
                    SweetAlert().showAlert("对不起", subTitle: "请检查您的网络配置", style: AlertStyle.Error)
                case 3840:
                    SweetAlert().showAlert("对不起", subTitle: "服务出现故障，请联系管理员", style: AlertStyle.Error)
                case -1001 :
                    
                    SweetAlert().showAlert("对不起", subTitle: "上传信息超时", style: AlertStyle.Error)
                default:
                    SweetAlert().showAlert("对不起", subTitle: "请检查您的网络配置", style: AlertStyle.Error)
                }
                return }
            QL2(JSONDIC)
            let resultDict = JSONDIC as? NSDictionary
            let jsonObj = JSON(JSONDIC)
            let errorCode = jsonObj["errorcode"].intValue
            let errorMsg = jsonObj["message"].stringValue
            QL2(errorMsg)
            if errorCode != 1 {
                SweetAlert().showAlert("对不起", subTitle: "\(errorMsg)", style: AlertStyle.Warning)
            }else{
                self.dismissViewControllerAnimated(true, completion: nil)
            }
        }
    }
    func clickLeftBtn() {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
        // 回车时退出编辑
        if text == "\n"
        {
            textView.resignFirstResponder()
            
            return true
        }
        return true
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    }
}